package edu.cuit.project.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import edu.cuit.project.dao.DepartmentDAO;
import edu.cuit.project.dao.EvaluateDAO;
import edu.cuit.project.dao.ExpertDAO;
import edu.cuit.project.dao.ExpertteamDAO;
import edu.cuit.project.dao.NationDAO;
import edu.cuit.project.dao.ProjectDAO;
import edu.cuit.project.dao.UserDAO;
import edu.cuit.project.domain.Department;
import edu.cuit.project.domain.Evaluate;
import edu.cuit.project.domain.Expert;
import edu.cuit.project.domain.Expertteam;
import edu.cuit.project.domain.Nation;
import edu.cuit.project.domain.Project;
import edu.cuit.project.domain.Qualification;
import edu.cuit.project.domain.User;
import edu.cuit.project.factory.BeanFactory;

public class UserService implements IUserService{
	

	@Override
	public List<Expert> getExpertList(int page) {
		
		return null;
	}

	@Override
	public List<Evaluate> getEvaluates(Expert expert) {
		List<Evaluate> list=new ArrayList<Evaluate>();
		list.addAll(expert.getEvaluates());
		return list;
	}

	@Override
	public boolean addExpert(Expert expert) {
		
			 BeanFactory.getDAO(ExpertDAO.class).save(expert);
			 return true;
		
		
	}

	@Override
	public boolean login(User user) {
		
		List list=BeanFactory.getDAO(UserDAO.class).findByUsername(user.getUsername());
		if(list!=null&&list.size()>0&&((List<User>)list).get(0).getPassword().equals(user.getPassword()))
		return true;
		else
			return false;
	}

	@Override
	public boolean updateExpertInfo(Expert expert) {
		// TODO Auto-generated method stub
		 BeanFactory.getDAO(ExpertDAO.class).merge(expert);
		 return true;
	}

	@Override
	public boolean deleteExpert(Expert expert) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean addExpertToTeam(Expert expert, Expertteam expertteam) {
		
		expertteam.addExpert(expert);
		 BeanFactory.getDAO(ExpertteamDAO.class).merge(expertteam);
		 return true;
	}

	@Override
	public boolean deleteExpertFromTeam(Expert expert, Expertteam expertteam) {
	
		return expertteam.getExperts().remove(expert);
	}

	@Override
	public boolean postEvaluateToExpert(Evaluate evaluate) {
		
		 try {
			BeanFactory.getDAO(EvaluateDAO.class).save(evaluate);
			
		} catch (Exception e) {
			
			return false;
		}
		 return true;
	}

	@Override
	public boolean updateEvaluateToExpert(Evaluate evaluate) {
		 try {
			BeanFactory.getDAO(EvaluateDAO.class).merge(evaluate);
		} catch (Exception e) {
			return false;
		}
		
		return true;
	}

	@Override
	public User getUser(String username) {
	
		try {
			return ((List<User>) BeanFactory.getDAO(UserDAO.class).findByUsername(username)).get(0);
		} catch (Exception e) {
			return null;
		}
		
	}

	@Override
	public List<Project> getProjectAll() {
	
		return  BeanFactory.getDAO(ProjectDAO.class).findAll();
	}

	@Override
	public List<Nation> getNationAll() {
	
		return BeanFactory.getDAO(NationDAO.class).findAll();
	}

	@Override
	public List<Department> getDepartmentAll() {
		
		return BeanFactory.getDAO(DepartmentDAO.class).findAll();
	}

	@Override
	public List<Expert> getExpertAll() {
		
		return BeanFactory.getDAO(ExpertDAO.class).findAll();
	}

	@Override
	public List<Expert> getExpertByProject(Project project) {
		
		List<Expert> lists=new ArrayList<Expert>();
		
		lists.addAll(project.getExpertteam().getExperts());
		return lists;
	}




	@Override
	public List<Expert> getExpertByCate(Department d, int education,
			Qualification q) {
		
		Session session=BeanFactory.getDAO(ExpertDAO.class).getSession();
		Criteria criteria=session.createCriteria(Expert.class);
		if(!d.getId().equals("00")){
			criteria.add(Restrictions.eq("department", d));
		}
		
		if(education!=0){
			criteria.add(Restrictions.eq("education", education));
		}
		
		if(!q.getId().equals("1")){
			criteria.add(Restrictions.eq("qualification", q));
		}
		
		return criteria.list();
	}

	@Override
	public List<Expert> getExpertByCateForTeam(Department d, int education,
			Qualification q,Expertteam expertTeam) {
		Session session=BeanFactory.getDAO(ExpertDAO.class).getSession();
		Criteria criteria=session.createCriteria(Expert.class);
		if(!d.getId().equals("00")){
			criteria.add(Restrictions.eq("department", d));
		}
		
		if(education!=0){
			criteria.add(Restrictions.eq("education", education));
		}
		
		if(!q.getId().equals("1")){
			criteria.add(Restrictions.eq("qualification", q));
		}
		List<Expert> list=criteria.list();
		for (Expert expert : expertTeam.getExperts()) {
			list.remove(expert);
		}
		return list;
	
	}
	@Override
	public List<Expert> search(String words){
		Session session=BeanFactory.getDAO(ExpertDAO.class).getSession();
		Criteria criteria=session.createCriteria(Expert.class);
		criteria.add(Restrictions.like("name", words));
		return criteria.list();
	}
	@Override
	public Evaluate getEvaluate(String expertId, Project project) {
		Expert expert=new Expert();
		expert.setId(expertId);
		Session session=BeanFactory.getDAO(ExpertDAO.class).getSession();
		Criteria criteria=session.createCriteria(Evaluate.class);
		
		criteria.add(Restrictions.eq("expert", expert));
		criteria.add(Restrictions.eq("project", project));
		criteria.setFlushMode(FlushMode.ALWAYS);
		List<Evaluate> list=criteria.list();
		if(list.size()>0){
			return list.get(0);
		}else{
			Evaluate mEvaluate=new Evaluate();
			mEvaluate.setExpert(expert);
			mEvaluate.setProject(project);
			return mEvaluate;
		}
		
	}
}
